package com.jxc.dao.impl;

import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.jxc.dao.LibraryDao;
import com.jxc.entity.Library;
import com.jxc.util.PageBean;

public class LibraryDaoImpl extends HibernateDaoSupport implements LibraryDao {

	public void deleteLibrary(Library library) {

		this.getHibernateTemplate().delete(library);
	}
	public Library getLibrary(Library library) {
		this.getHibernateTemplate().get(Library.class, library.getId());
		return null;
	}

	public PageBean getLibraryList(final String hql,final PageBean pb) {
		this.getHibernateTemplate().execute(new HibernateCallback() {
			
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				Query query=session.createQuery(hql);
				pb.setTotalitem(query.list().size());
				query.setFirstResult((pb.getPageon()-1)*pb.getPagesize());
				query.setMaxResults(pb.getPagesize());
				pb.getTotalpage();
				pb.setList(query.list());
				return pb;
			}
		});
		return pb;
	}

	public void insertLibrary(Library library) {

		this.getHibernateTemplate().save(library);
		
	}

	public void updateLibrary(Library library) {

		this.getHibernateTemplate().merge(library);
	}

}
